using System;
using System.Collections.Generic;
using System.IO;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using FilteredStatistics.Common.Controller;
using FilteredStatistics.View;

namespace FilteredStatistics.Data
{
    class RemoveSmallTimesPostProcessFilter : IPostProcessFilter
    {
        public RemoveSmallTimesPostProcessFilter(Stream stream, DataVersion version)
        {
            Deserialize(stream, version);
        }

        public RemoveSmallTimesPostProcessFilter(TimeSpan minTime)
        {
            m_MinTime = minTime;
        }

#region IPostProcessFilter Members

        public override PostProcessType Type
        {
            get { return PostProcessType.RemoveSmallTimes; }
        }

        public override String DisplayString
        {
            get
            {
                return String.Format(PluginView.GetLocalizedString("RemoveSmallTimesPPText"),
                                     m_MinTime.Hours, m_MinTime.Minutes, m_MinTime.Seconds);
            }
        }

        public override List<ActivityRangeInfoCache> FilterResults(IList<ActivityRangeInfoCache> rangesToFilter, IActivity activity)
        {
            List<ActivityRangeInfoCache> result = new List<ActivityRangeInfoCache>();

            foreach (ActivityRangeInfoCache range in rangesToFilter)
            {
                if (FilteredStatisticsStatisticsProvider.Instance.GetElapsedTimeValue(range) > m_MinTime)
                {
                    result.Add(range);
                }
            }

            return result;
        }

        public override int CompareTo(IPostProcessFilter filter)
        {
            if (filter is RemoveSmallDistancesPostProcessFilter ||
                filter is RemoveSmallTimesPostProcessFilter)
            {
                return 0;
            }
            else if (filter is MergeNearDistancesPostProcessFilter ||
                     filter is MergeNearTimesPostProcessFilter)
            {
                return 1;
            }

            return -1;
        }

#endregion

#region IPluginSerializable members

        public override void Serialize(Stream stream)
        {
            base.Serialize(stream);

            SerializableTimeSpan minTime = new SerializableTimeSpan(m_MinTime);

            minTime.Serialize(stream);
        }

#endregion

#region Deserialization methods

        public override void Deserialize_V2(Stream stream, DataVersion version)
        {
            SerializableTimeSpan minTime = new SerializableTimeSpan(0);

            minTime.Deserialize(stream, version);

            m_MinTime = minTime;
        }

#endregion

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            RemoveSmallTimesPostProcessFilter filter = obj as RemoveSmallTimesPostProcessFilter;

            if (filter != null)
            {
                return MinTime == filter.MinTime;
            }

            return base.Equals(obj);
        }

        public TimeSpan MinTime
        {
            get { return m_MinTime; }
        }

        private TimeSpan m_MinTime;
    }
}
